<?php
    namespace Home\Model;
    use Think\Model;
/**
 * Description of GoodsModel
 *
 * 商品数据表相关数据操作
 * 
 * @author Mitol
 */
class GoodsModel extends Model {
    
    /*
     * 增加商品
     */
    public function addGoods($data) {
        if(!empty($data)) {
            $this->create();       //获取表单数据，创建数据
            $result = $this->add($data);
            return $result;
        } else {
            return FALSE;
        }
    }
   
    /*
     * 获取商品信息
     */
    public function deleteByID($data) {
        return $this->where( $data )->delete();
    }
    
    /*
     * 获得商品列表
     */
    public function queryAll() {
        return $this->select();
    }
    
    /*
     * 查询商品信息依据goodsID
     */
    public function queryByGoodsID($goodsID) {
        return $this->where("id=".$goodsID)->select();
    }
    
    /*
     * 按照品牌查询
     */
    public function queryByBrandID($brandID) {
        return $this->where("brandType_ID=".$brandID)->select();
    }
    
    /*
     * 按照商品类型查询，是否是拼购商品/购物商品
     */
    public function queryByTypeID($typeID) {
        return $this->where('id='.$typeID)->select();
    }
    
    /*
     * 按照收藏数量排序查询
     */
    public function queryByCollect() {
        return $this->order('collectNumber desc')->select();
    }
    /*
     * 全模糊查询
     */
    public function queryByLikeAll($words) {
        //todo 模糊查询多字段实现
        return $this->where('goodsName like %'.$words.'%')->order('purchaseNumber desc')->select();
    }
    
    /*
     * 更新记录
     */
    public function updateByGoodsID($goodsID, $data) {
        return $this->where('id='.$goodsID)->save($data);
    }
    
    /*
     * 更新点击量
     */
    public function updateClickNumber($goodsID) {
        if(!empty($goodsID)) {
            $sql = "update t_goods set clickNumber = clickNumber+1 where goods_id=".$goodsID;
            return $this->query($sql);
        } else {
            return FALSE;
        }
    }
        
    /*
     * 更新收藏数量 type： <=0减，>0加
     */
    public function updateColletNumber($goodsID, $type) {
        if(!empty($goodsID)) {
            if($type>0) {
                $sql = "update t_goods set collectNumber = collectNumber+1 where goods_id=".$goodsID;
            } else {
                $sql = "update t_goods set collectNumber = collectNumber-1 where goods_id=".$goodsID;
            }
            return $this->query($sql);
        } else {
            return FALSE;
        }
    }
    
    /*
     * 更新购买数量
     */
    public function updatePurchaseNumber($goodsID) {
        if(!empty($goodsID)) {
            $sql = "update t_goods set purchaseNumber = purchaseNumber+1 where goods_id=".$goodsID;
            return $this->query($sql);
        } else {
            return FALSE;
        }
    }
        
        
    
    /*
     * 上架商品
     */
    public function shelve() {
    }
    
    /*
     * 下架商品
     */
    public function unshelve(){}
}
